<?php
	/**
	 * 银行转账
	 */
    class Transfer extends APIBase {
		
		public $tag = "Transfer";
		public $isLogin = true;
		public $isvip = true;
		
		public function before() 
		{		
		    //$this->initCacheRedis(); 	
			$this->initMysql();
			$this->inithttp();
			return true;
		}

		public function logic() 
		{			
			
			if (!isset($this->param['send_money']))
			{
				$this->returnError(403, "send_money is must need");
			}
			else
			{
				$money_count = (int)$this->param['send_money'];
			
				if ( $money_count <= 0 )
				{
					$this->returnError(404, "send money must more than 0");
				}
			}
			
			$player = $this->data_redis->hMget("hu:{$this->uid}", array('user','bank', 'money','bank_password','bank_salt'));
			
				
			if ($player['bank'] < $money_count)
			{
				$this->returnError(405, "send money more than self money");
			}
			
			if (!isset($this->param['bank_pass']))
			{
				$this->returnError(406, "bank_pass is must need");
			}
			else
			{
				$bank_pass = $this->param['bank_pass'];
			   
				if (md5($bank_pass.$player['bank_salt'])!=$player['bank_password'])
				{
					$this->returnError(407, "bank_password is false");
				}
			}
			
			$re_info =array();//接收转账日记变量
			$se_info =array();//发送金币
			
			if (!isset($this->param['receive_uid']))
			{
				$this->returnError(401, "receive_uid is must need");
			}
			else 
			{
				$receive_uid = (int)$this->param['receive_uid'];
				
				//查找缓存中是否有收红包玩家信息，如果没有就查询数据库
				//$isExists = $this->data_redis->exists("hu:{$receive_uid}");
				$this->getDataRedis($receive_uid);
				$isExists = $this->get_redis->exists("hu:{$receive_uid}");
							
			   
				if (!$isExists ) 
				{										
					$row = $this->mysql->find("select * from player where id = $receive_uid");
					
					if (count($row) == 0) 
					{
						$this->returnError(402, 'user is not exist.');
					}
					
					$receive = $row[0];		

					//转移金币
					$player['bank'] -= $money_count;
					$receive['bank'] += $money_count;
					
					
			        $re_info['RoomID'] = 0;
			        $re_info['ChangeType'] = 3;
                    $re_info['CollectTime']  = time();
					$re_info['UserID']  = $receive_uid;
			        $re_info['MoneyInBank']  =  $money_count + $receive['bank'];
			        $re_info['MoneyInRoom']  =  $receive['money'];
			        $re_info['OutMoney']     = 0;
			        $re_info['InMoney']      = $money_count;
					
					
					//更新数据库
					$where['id'] = $receive_uid;
					$sign  = $this->mysql->update('player', $receive, $where);
					//$wybs =uniqid();
					//保存到redis里
					//$this->cache_redis->hMset("bank_rec:$wybs",$re_info);		
					$result = $this->mysql->insert("change_money_rec",$re_info); 			
				}
				else 
				{	
					$receive = $this->get_redis->hMGet("hu:{$receive_uid}", array('bank','money'));		
                    
					//转移金币
					$player['bank'] -= $money_count;
					$receive['bank'] += $money_count;
					
					$re_info['type'] = 4;
			        $re_info['vid'] = 1;
                    $re_info['tid']  = 0;
					$re_info['seatid']  = 0;
			        $re_info['uid']  =  (int)$receive_uid;
					
					$re_info['money'] = $money_count + $receive['bank'];
			        $re_info['change_money']  =  $receive['money'];
			        $re_info['create_time']     = time();
			     
					//更新缓存
					$this->get_redis->hMset("hu:{$receive_uid}", $receive);
					$this->get_redis->close();
					//
					//$wybs =uniqid();
					//$this->cache_redis->hMset("bank_rec:$wybs",$re_info);	
					$result = $this->mysql->insert("change_money_rec",$re_info); 
					
					
				}
			}
			
			//bank_rec			
			//更新缓存
			//			
			$this->data_redis->hMset("hu:{$this->uid}", $player);			
			//玩家日记记录
			$se_info['type'] = 3;
			$se_info['vid'] = 1;
            $se_info['tid']  = 0;
			$se_info['seatid']  = 0;
			$se_info['uid']  = $this->uid;
			$se_info['money']  =  $player['money'];
			$se_info['change_money']     = -$money_count;
			$se_info['create_time']      = time();
			$result = $this->mysql->insert("change_money_rec",$se_info); 
			//通知玩家
			//$title = "收到红包啦";
			//$content = $player['user'] . "派发了包含" . $money_count . "金币的红包给你";
			//$this->sendMessage($friend_uid, $title, $content);
			
			//$title2 = "派送红包成功";
			//$content2 = "成功派发了包含" . $money_count . "金币的红包给" . $friend['user'];
			//$this->sendMessage($this->uid, $title2, $content2);
			
			//$result = $this->data_redis->hMget("$this->uid", array('money'));

			//$result['money'] = (int)$result['money'];

			//$this->returnData('0','success!');
			unset($player['bank_password']);
			unset($player['bank_salt']);
			//通知玩家
			$url = 'http://'.Config::$ip_server_confing['ip'].':'.Config::$ip_server_confing['myport'].'/pushsrv?action=Transfer&receiver='.$receive_uid.'&param={"user":"'.$player['user'].'","re_money":"'.$money_count.'"}';
			
			
			
			if(!$this->http->get($url)){
				$this->returnError(577, "消息发送失败！");
			 }	
			
			$this->returnData($player,0, "转账成功！");
		}
		
    	public function after() {
			
			//$this->deinitCacheRedis();
			$this->deinitDataRedis();
			$this->deinitMysql();
			$this->deinithttp();
		}
    }
?>